// page_two/drugcc/drugcc.js
let app = getApp()
let yuming = app.globalData.yuming
let fuhao = app.globalData.fuhao
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    jiance_shebei_list: [],
    heights: "",
    title: '',
    buttonClicked: false,
    // huan: ['温湿度设备', '阴凉柜监测', '冷藏柜监测'],
    huan: [{
      name: '温湿度设备',
      stats: ''
    }, {
      name: '阴凉柜监测',
      stats: 2
    }, {
      name: '冷藏柜监测',
      stats: 1
    }],
    stats: '',
    // 冷藏/阴凉（0其他，1冷藏，2阴凉）
    currtab: 0,
    gongwu: false,
    // sbList: [{
    //   createtime: '2021-08-25',
    //   sbname: '设备',
    //   sbbh: 'AC1243',
    //   scph: '210914-1',
    //   sccs: 'xxx厂',
    //   sbhgz: `${yuming}static/applet/images/xiaofei/images/linshi/wem.jpg`,
    //   sblz: `${yuming}static/applet/images/xiaofei/images/linshi/wyn.jpg`
    // }],
    sbList: [],
    sbname: '',
    sbbh: '',
    scph: '',
    sccs: '',
    sbhgz: '',
    wusbhgz: '',
    sblz: '',
    wusblz: '',
    page: 1,
    yiyao: '',
    keywords: '',
    satrtime: '',
    endtime: '',
    istime: false,
    satrtimes: '开始时间',
    endtimes: '结束时间',
    yinliang_list: [],
    leng_list: [],
    iswsd: false,
    isylg: false,
    islcg: false,
    total: ''
  },
  // 库存列表点击进销售
  xiao_xq(e) {
    var drugs = e.currentTarget.dataset.drugs;
    wx.navigateTo({
      url: '/wenshidu/xiaonxq/xiaonxq?drugs=' + drugs + "&state=1",
    })
  },
  tianjia() {
    wx.navigateTo({
      url: '/page_two/addjiance/addjiance?title=添加设备&state=0&isxiu=1',
    })
  },
  // 查看更多
  lookmore() {
    this.setData({
      page: (this.data.page) + 1,
    })
    if (this.data.currtab == 0) {
      shuzong = this.data.jiance_shebei_list
    }
    if (this.data.currtab != 0) {
      shuzong = this.data.yinliang_list
    }
    // if (this.data.currtab == 2) {
    //   shuzong = this.data.leng_list
    // }
    this.getccsbList()
  },
  qies(e) {
    shuzong = []
    shu = []
    var index = e.currentTarget.dataset.currtab
    var stats = e.currentTarget.dataset.stats
    this.setData({
      currtab: index,
      page: 1,
      stats: stats,
      sbname: '',
      iswsd: false,
      isylg: false,
      keywords: '',
      islcg: false,
      sbbh: '',
      scph: '',
      sccs: '',
      sbhgz: '',
      wusbhgz: '',
      sblz: '',
      wusblz: '',
      gongwu: false,
    })
    this.getccsbList()
  },
  tan() {
    this.setData({
      istime: !this.data.istime
    })
  },
  queren() {
    this.getccsbList()
    this.setData({
      istime: !this.data.istime
    })
  },
  // 选择开始时间
  bindDateChange: function (e) {
    this.setData({
      satrtime: e.detail.value,
      satrtimes: e.detail.value,
    })
  },
  // 结束
  endTime(e) {
    this.setData({
      endtime: e.detail.value,
      endtimes: e.detail.value,
    })
  },
  sousuoaa(e) {
    this.setData({
      keywords: e.detail.value,
      page: 1
    })
    shuzong = []
    shu = []
    this.getccsbList()
    if (this.data.currtab == 0) {
      // this.lenglian()

    } else {

    }
  },
  // 药品品种
  lenglian() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Shop/shop_jxckc_alllist', {
      // this.myRequest(yuming + 'Shop/shop_jxc_tw_alllist', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      satrtime: that.data.satrtime,
      endtime: that.data.endtime,
      drugs_type: 3,
      keywords: that.data.keywords
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        }
        that.setData({
          sbList: shuzong.concat(shu)
        })
      }
    })
  },
  // 存储设备列表接口
  getccsbList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoDeviceList', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      keywords: that.data.keywords,
      shade_cold: that.data.stats
    }, "POST").then(res => {
      this.setData({
        total: res.data.total
      })
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        // shu.forEach(item => {
        //   if (item.humidity.length != 0) {
        //     item.humidity.detail.time = this.formatDate(new Date(item.humidity.detail.time))
        //   }
        // })
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true,
              iswsd: true,
              isylg: true,
              islcg: true,
            })
          }
        }
        if (that.data.currtab == 0) {
          that.setData({
            jiance_shebei_list: shuzong.concat(shu)
          })
        } else {
          that.setData({
            yinliang_list: shuzong.concat(shu)
          })
        }

        // var yinliang_list = []
        // var leng_list = []
        // 冷藏/阴凉（0其他，1冷藏，2阴凉）
        // that.data.jiance_shebei_list.forEach(item => {
        //   if (item.shade_cold == 2 || item.shade_cold == null) {
        //     yinliang_list.push(item)
        //   }
        //   if (item.shade_cold == 1) {
        //     leng_list.push(item)
        //   }
        // })
        // that.setData({
        //   yinliang_list: yinliang_list,
        //   leng_list: leng_list
        // })
      }
    })
  },
  // 查看更多
  // lookmore() {
  //     shuzong = this.data.sbList
  //     this.setData({
  //         page: (this.data.page) + 1,
  //     })
  //     this.getccsbList();
  // },
  // 设备名称
  sbname(e) {
    this.setData({
      sbname: e.detail.value
    })
  },
  // 设备编号
  sbbh(e) {
    this.setData({
      sbbh: e.detail.value
    })
  },
  // 生产批号
  scph(e) {
    this.setData({
      scph: e.detail.value
    })
  },
  // 生产厂商
  sccs(e) {
    this.setData({
      sccs: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa
    var that = this;
    //选取图片
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //设备合格证
        if (aaa == 1) {
          that.setData({
            sbhgz: urls,
            wusbhgz: data.imgUrl
          })
        }
        // 设备留证
        if (aaa == 2) {
          that.setData({
            sblz: urls,
            wusblz: data.imgUrl
          })
        }
      })
    })
  },
  // 点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 验证
  ti() {
    if (this.data.sbname == '') {
      wx.showToast({
        title: '请输入设备名称',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sbname))) {
      wx.showToast({
        title: '设备名称格式错误',
        icon: 'none'
      })
      this.setData({
        sbname: ''
      })
    } else if (this.data.sbbh == '') {
      wx.showToast({
        title: '请输入设备编号',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sbbh))) {
      wx.showToast({
        title: '设备编号格式错误',
        icon: 'none'
      })
      this.setData({
        sbbh: ''
      })
    } else if (this.data.scph == '') {
      wx.showToast({
        title: '请输入生产批号',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.scph))) {
      wx.showToast({
        title: '生产批号格式错误',
        icon: 'none'
      })
      this.setData({
        scph: ''
      })
    } else if (this.data.sccs == '') {
      wx.showToast({
        title: '请输入生产厂商',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sccs))) {
      wx.showToast({
        title: '生产厂商格式错误',
        icon: 'none'
      })
      this.setData({
        sccs: ''
      })
    } else if (this.data.wusbhgz == '') {
      wx.showToast({
        title: '请上传设备合格证',
        icon: 'none'
      })
    } else if (this.data.wusblz == '') {
      wx.showToast({
        title: '请上传设备留证',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加设备
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Yiyao/yiyaoDeviceAdd', {
      shopId: wx.getStorageSync('shopId'),
      name: that.data.sbname,
      devcode: that.data.sbbh,
      pihao: that.data.scph,
      changshang: that.data.sccs,
      hege_img: that.data.wusbhgz,
      liuzheng_img: that.data.wusblz,
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.getccsbList();
        setTimeout(function () {
          that.setData({
            currtab: 0,
            buttonClicked: false,
            sbname: '',
            sbbh: '',
            scph: '',
            sccs: '',
            sbhgz: '',
            wusbhgz: '',
            sblz: '',
            wusblz: '',
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }

    })

  },
  // 检测设备详情
  jiance_xq(e) {
    var id = e.currentTarget.dataset.id
    var item = e.currentTarget.dataset.item
    var isxiu = e.currentTarget.dataset.isxiu
    wx.setStorageSync('shebeixq', item)

    if (isxiu == 0) {
      wx.navigateTo({
        url: '/page_two/addjiance/addjiance?title=设备详情&state=1&she_id=' + id + "&isxiu=" + isxiu,
      })
    } else {
      wx.navigateTo({
        url: '/page_two/addjiance/addjiance?title=修改设备&state=1&she_id=' + id + "&isxiu=" + isxiu,
      })
    }

  },
  // 温湿度详情
  ws_xq(e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    var wenid = e.currentTarget.dataset.wenid
    var humid = e.currentTarget.dataset.humid
    var humidity_type = e.currentTarget.dataset.item.humidity_type
    console.log(humidity_type)

    var shade_cold = e.currentTarget.dataset.shade_cold
    var item = e.currentTarget.dataset.item
    var isxiu = e.currentTarget.dataset.isxiu
    wx.setStorageSync('shebeixq', item)
    console.log(humid)
    if (humid == '' || humid == null) {
      wx.showModal({
        title: '您还没有配置哦~',
        cancelText: '取消',
        confirmText: '前往',
        success: function (res) {
          if (res.confirm) {
            console.log("确定")
            wx.navigateTo({
              url: '/page_two/addjiance/addjiance?title=修改设备&state=1&she_id=' + id + "&isxiu=" + isxiu,
            })
          } else {

            console.log("取消")
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/wenshidu/wen_listxq/wen_listxq?she_id=' + id + "&wenid=" + wenid + "&shade_cold=" + shade_cold + "&humidity_type=" + humidity_type,
      })
    }

  },
  // 删除
  shanchu(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoDeviceDel', {
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        shuzong = [];
        shu = [];
        that.getccsbList();
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      shopType: options.shopType,
      title: options.title
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      yiyao: wx.getStorageSync('stryId')
    })
    // if (this.data.yiyao != 6) {
    this.getccsbList(); //存储设备
    // this.lenglian()
    // } else {
    //   this.lenglian();
    // }
  },
  formatTen: function (num) {
    return num > 9 ? (num + "") : ("0" + num);
  },
  formatDate: function (date) {
    console.log(date)
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return year + "-" + this.formatTen(month) + "-" + this.formatTen(day);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})